const { data } = require("jquery");

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xc/dynamics/posts/index' + location.search,
                    add_url: 'xc/dynamics/posts/add',
                    edit_url: 'xc/dynamics/posts/edit',
                    del_url: 'xc/dynamics/posts/del',
                    multi_url: 'xc/dynamics/posts/multi',
                    import_url: 'xc/dynamics/posts/import',
                    table: 'xc_dynamics_posts',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'info.name', title: __('In_id') },
                        { field: 'userinfo.name', title: __('User_id') },

                        // { field: 'po_user_id', title: __('Po_user_id') },
                        // { field: 'po_id', title: __('Po_id') },
                        // { field: 'po_user_nickname', title: __('Po_user_nickname'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'type', title: __('Type'), searchList: { "1": __('评论'), "2": __('回复') }, formatter: Table.api.formatter.normal },
                        { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('回复'),
                                    title: __('回复'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'xc/dynamics/posts/posts?po_id=' + row.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), { title: "回传数据" });
                                    }
                                }],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        posts: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xc/dynamics/posts/posts' + location.search,
                    add_url: 'xc/dynamics/posts/add',
                    edit_url: 'xc/dynamics/posts/edit',
                    del_url: 'xc/dynamics/posts/del',
                    multi_url: 'xc/dynamics/posts/multi',
                    import_url: 'xc/dynamics/posts/import',
                    table: 'xc_dynamics_posts_posts',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'info.name', title: __('In_id') },
                        { field: 'userinfo.name', title: __('User_id') },

                        { field: 'userinfo.name', title: __('Po_user_id') },
                        { field: 'po_id', title: __('Po_id'), visible: false },
                        // { field: 'po_user_nickname', title: __('Po_user_nickname'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'type', title: __('Type'), searchList: { "1": __('评论'), "2": __('回复') }, formatter: Table.api.formatter.normal },
                        { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
